<!doctype html>
<html lang="zh-CN">
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <meta name="referrer" content="no-referrer-when-downgrade">
    

    <title>go之nginx日志分析 | Sang的博客</title>
    <meta property="og:title" content="go之nginx日志分析 - Sang的博客">
    <meta property="og:type" content="article">
        
    <meta property="article:published_time" content='2021-11-23T11:32:18&#43;08:00'>
        
        
    <meta property="article:modified_time" content='2021-11-23T11:32:18&#43;08:00'>
        
    <meta name="Keywords" content="golang,go语言,shell,k8s,博客,python,软件架构,公众号">
    <meta name="description" content="go之nginx日志分析">
        
    <meta name="author" content="仨哥">
    <meta property="og:url" content="https://stto_32.gitee.io/site/post/go%E4%B9%8Bnginx%E6%97%A5%E5%BF%97%E5%88%86%E6%9E%90/">
    <link rel="shortcut icon" href='/site/favicon.ico'  type="image/x-icon">

    <link rel="stylesheet" href='/site/css/normalize.css'>
    <link rel="stylesheet" href='/site/css/style.css'>
    <script type="text/javascript" src="//cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    
    
    
        <link href="https://cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css" rel="stylesheet">
    
    
    
    
        <link rel="stylesheet" href='/site/css/douban.css'>
    
        <link rel="stylesheet" href='/site/css/other.css'>
    
</head>


<body>
    <header id="header" class="clearfix">
    <div class="container">
        <div class="col-group">
            <div class="site-name ">
                
                    <a id="logo" href="https://stto_32.gitee.io/site">
                        Sang的博客
                    </a>
                
                <p class="description">专注于Python、Go语言(golang)、云原生</p>
            </div>
            <div>
                <nav id="nav-menu" class="clearfix">
                    <a class="current" href="https://stto_32.gitee.io/site">首页</a>
                    
                    <a  href="https://stto_32.gitee.io/site/tools/" title="小工具">小工具</a>
                    
                    <a  href="https://stto_32.gitee.io/site/archives/" title="归档">归档</a>
                    
                    <a  href="https://stto_32.gitee.io/site/about/" title="关于">关于</a>
                    
                </nav>
            </div>
        </div>
    </div>
</header>

    <div id="body">
        <div class="container">
            <div class="col-group">

                <div class="col-8" id="main">
                    
<div class="res-cons">
    
    <article class="post">
        <header>
            <h1 class="post-title">go之nginx日志分析</h1>
        </header>
        <date class="post-meta meta-date">
            2021年11月23日
        </date>
        
        
        <div class="post-meta">
            <span id="busuanzi_container_page_pv">|<span id="busuanzi_value_page_pv"></span><span>
                    阅读</span></span>
        </div>
        
        
        <div class="post-content">
            <p>go之nginx日志分析,够查看某一段时间内nginx日志访问情况，并且错误状态码超过一定比列就告警</p>
<blockquote>
<p>够查看某一段时间内nginx日志访问情况，并且错误状态码超过一定比列就告警</p>
</blockquote>
<p>功能实现</p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 16
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 17
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 18
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 19
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 20
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 21
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 22
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 23
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 24
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 25
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 26
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 27
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 28
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 29
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 30
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 31
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 32
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 33
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 34
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 35
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 36
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 37
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 38
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 39
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 40
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 41
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 42
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 43
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 44
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 45
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 46
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 47
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 48
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 49
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 50
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 51
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 52
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 53
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 54
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 55
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 56
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 57
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 58
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 59
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 60
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 61
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 62
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 63
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 64
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 65
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 66
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 67
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 68
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 69
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 70
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 71
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 72
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 73
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 74
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 75
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 76
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 77
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 78
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 79
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 80
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 81
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 82
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 83
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 84
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 85
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 86
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 87
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 88
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 89
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 90
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 91
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 92
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 93
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 94
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 95
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 96
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 97
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 98
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 99
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">100
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">101
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">102
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">103
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">104
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">105
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">106
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">107
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">108
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">109
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">110
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">111
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">112
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">113
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">114
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">115
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">116
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">117
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">118
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">119
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">120
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">121
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">122
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">123
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">124
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">125
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">126
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">127
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">128
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">129
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">130
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">131
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">132
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">133
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">134
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">135
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">136
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">137
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">138
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">139
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">140
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">141
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">142
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">143
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">144
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">145
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">146
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">147
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">148
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">149
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">150
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">151
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">152
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">153
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">154
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">155
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">156
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">157
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">158
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">159
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">160
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">161
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">162
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">163
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">164
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">165
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">166
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">167
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">168
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">169
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">170
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">171
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">172
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">173
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">174
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">175
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">176
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">177
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">178
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">179
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">180
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">181
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">182
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">183
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">184
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">185
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">186
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">187
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">188
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">189
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">190
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">191
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">192
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">193
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">194
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">195
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">196
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">197
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">198
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">199
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">200
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">201
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">202
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">203
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go">
<span style="color:#f92672">package</span> <span style="color:#a6e22e">tools</span>

<span style="color:#f92672">import</span> (
   <span style="color:#e6db74">&#34;bufio&#34;</span>

   <span style="color:#e6db74">&#34;encoding/json&#34;</span>
   <span style="color:#e6db74">&#34;fmt&#34;</span>

   <span style="color:#e6db74">&#34;github.com/shopspring/decimal&#34;</span>
   <span style="color:#e6db74">&#34;io&#34;</span>
   <span style="color:#e6db74">&#34;log&#34;</span>
   <span style="color:#e6db74">&#34;os&#34;</span>
   <span style="color:#e6db74">&#34;regexp&#34;</span>
   <span style="color:#e6db74">&#34;sort&#34;</span>
   <span style="color:#e6db74">&#34;strconv&#34;</span>
   <span style="color:#e6db74">&#34;time&#34;</span>
)
<span style="color:#66d9ef">var</span> (
   <span style="color:#a6e22e">LOGFILE</span> = <span style="color:#e6db74">&#34;D:\work\perlearn\go_space\total\access.log&#34;</span>
   <span style="color:#a6e22e">nf</span>           <span style="color:#a6e22e">NginxFields</span>
   <span style="color:#a6e22e">errCounter</span>   <span style="color:#66d9ef">int</span> <span style="color:#75715e">//错误状态码数量
</span><span style="color:#75715e"></span>   <span style="color:#a6e22e">totalCounter</span> <span style="color:#66d9ef">int</span> <span style="color:#75715e">//总共状态码数量
</span><span style="color:#75715e"></span>   <span style="color:#a6e22e">MapIpNums</span>  =  make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">int</span>) <span style="color:#75715e">//map 客户端ip：数量
</span><span style="color:#75715e"></span>   <span style="color:#a6e22e">MapUrlNums</span> =  make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">int</span>) <span style="color:#75715e">//map url：数量
</span><span style="color:#75715e"></span>   <span style="color:#a6e22e">StatusMap</span> = make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">interface</span>{}) <span style="color:#75715e">// {&#34;total&#34;: {&#34;200&#34;: &#34;50&#34;, &#34;101&#34;: &#34;179&#34;, &#34;500&#34;: &#34;390&#34;,&#34;404&#34;:&#34;290&#34;}}
</span><span style="color:#75715e"></span>   <span style="color:#a6e22e">tmp1</span>=make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">int</span>) <span style="color:#75715e">//{&#34;200&#34;: &#34;50&#34;}
</span><span style="color:#75715e"></span>   <span style="color:#a6e22e">Threshold</span>  = <span style="color:#ae81ff">0.01</span> <span style="color:#75715e">//百分比阀值，超过1就通知
</span><span style="color:#75715e"></span>   <span style="color:#a6e22e">Top</span>  <span style="color:#66d9ef">int</span>
   <span style="color:#a6e22e">TimeLong</span> <span style="color:#66d9ef">string</span> <span style="color:#75715e">//最近多长时间的，以分钟为单位
</span><span style="color:#75715e"></span>   <span style="color:#a6e22e">bt</span>,<span style="color:#a6e22e">et</span>  <span style="color:#66d9ef">int64</span>
   <span style="color:#a6e22e">regx</span> = <span style="color:#e6db74">&#34;(^127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.1[6-9]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.2[0-9]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.3[0-1]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^192\.168\.[0-9]{1,3}\.[0-9]{1,3}$)&#34;</span>


)
<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">init</span>()  {
   <span style="color:#a6e22e">rootCmd</span>.<span style="color:#a6e22e">PersistentFlags</span>().<span style="color:#a6e22e">StringVar</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">LOGFILE</span>, <span style="color:#e6db74">&#34;file&#34;</span>,<span style="color:#e6db74">&#34;D:\work\perlearn\go_space\total\access.log&#34;</span> , <span style="color:#e6db74">&#34;nginx access log  file &#34;</span>)
   <span style="color:#a6e22e">rootCmd</span>.<span style="color:#a6e22e">PersistentFlags</span>().<span style="color:#a6e22e">IntVar</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">Top</span>, <span style="color:#e6db74">&#34;top&#34;</span>, <span style="color:#ae81ff">20</span>, <span style="color:#e6db74">&#34;Top值，默认是20&#34;</span>)
   <span style="color:#a6e22e">rootCmd</span>.<span style="color:#a6e22e">PersistentFlags</span>().<span style="color:#a6e22e">StringVar</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">TimeLong</span>, <span style="color:#e6db74">&#34;long&#34;</span>, <span style="color:#e6db74">&#34;30&#34;</span>, <span style="color:#e6db74">&#34;最近多长时间，默认是30&#34;</span>)

}


<span style="color:#66d9ef">type</span> <span style="color:#a6e22e">NginxFields</span> <span style="color:#66d9ef">struct</span> {
   <span style="color:#a6e22e">Timestamp</span>         <span style="color:#66d9ef">string</span>   <span style="color:#e6db74">`json:&#34;@timestamp&#34;`</span>
   <span style="color:#a6e22e">RemoteAddr</span>        <span style="color:#66d9ef">string</span>    <span style="color:#e6db74">`json:&#34;remote_addr&#34;`</span>
   <span style="color:#a6e22e">HTTPXForwardedFor</span> <span style="color:#66d9ef">string</span>    <span style="color:#e6db74">`json:&#34;http_x_forwarded_for&#34;`</span>
   <span style="color:#a6e22e">HTTPHost</span>          <span style="color:#66d9ef">string</span>    <span style="color:#e6db74">`json:&#34;http_host&#34;`</span>
   <span style="color:#a6e22e">ServerPort</span>        <span style="color:#66d9ef">string</span>    <span style="color:#e6db74">`json:&#34;server_port&#34;`</span>
   <span style="color:#a6e22e">Scheme</span>            <span style="color:#66d9ef">string</span>    <span style="color:#e6db74">`json:&#34;scheme&#34;`</span>
   <span style="color:#a6e22e">RequestMethod</span>     <span style="color:#66d9ef">string</span>    <span style="color:#e6db74">`json:&#34;request_method&#34;`</span>
   <span style="color:#a6e22e">RequestURI</span>        <span style="color:#66d9ef">string</span>    <span style="color:#e6db74">`json:&#34;request_uri&#34;`</span>
   <span style="color:#a6e22e">Status</span>            <span style="color:#66d9ef">string</span>    <span style="color:#e6db74">`json:&#34;status&#34;`</span>
   <span style="color:#a6e22e">UpstreamAddr</span>      <span style="color:#66d9ef">string</span>    <span style="color:#e6db74">`json:&#34;upstream_addr&#34;`</span>
   <span style="color:#a6e22e">Authbid</span>           <span style="color:#66d9ef">string</span>    <span style="color:#e6db74">`json:&#34;AUTHBID&#34;`</span>
   <span style="color:#a6e22e">Apptoken</span>          <span style="color:#66d9ef">string</span>    <span style="color:#e6db74">`json:&#34;APPTOKEN&#34;`</span>
   <span style="color:#a6e22e">Authuid</span>           <span style="color:#66d9ef">string</span>    <span style="color:#e6db74">`json:&#34;AUTHUID&#34;`</span>
   <span style="color:#a6e22e">BodyBytesSent</span>     <span style="color:#66d9ef">string</span>    <span style="color:#e6db74">`json:&#34;body_bytes_sent&#34;`</span>
   <span style="color:#a6e22e">BytesSent</span>         <span style="color:#66d9ef">string</span>    <span style="color:#e6db74">`json:&#34;bytes_sent&#34;`</span>
   <span style="color:#a6e22e">RequestTime</span>       <span style="color:#66d9ef">string</span>    <span style="color:#e6db74">`json:&#34;request_time&#34;`</span>
   <span style="color:#a6e22e">RequestBody</span>       <span style="color:#66d9ef">string</span>    <span style="color:#e6db74">`json:&#34;request_body&#34;`</span>
   <span style="color:#a6e22e">HTTPReferer</span>       <span style="color:#66d9ef">string</span>    <span style="color:#e6db74">`json:&#34;http_referer&#34;`</span>
   <span style="color:#a6e22e">HTTPUserAgent</span>     <span style="color:#66d9ef">string</span>    <span style="color:#e6db74">`json:&#34;http_user_agent&#34;`</span>
   <span style="color:#a6e22e">Host</span>              <span style="color:#66d9ef">string</span>    <span style="color:#e6db74">`json:&#34;host&#34;`</span>
}
<span style="color:#75715e">//readNginxLog 读取文件
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">ReadNginxLog</span>()  {
      <span style="color:#a6e22e">currentTime</span><span style="color:#f92672">:=</span><span style="color:#a6e22e">time</span>.<span style="color:#a6e22e">Now</span>()
      <span style="color:#a6e22e">ago</span><span style="color:#f92672">:=</span><span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Sprintf</span>(<span style="color:#e6db74">&#34;-%sm&#34;</span>,<span style="color:#a6e22e">TimeLong</span>)
      <span style="color:#a6e22e">d</span>,<span style="color:#a6e22e">_</span><span style="color:#f92672">:=</span><span style="color:#a6e22e">time</span>.<span style="color:#a6e22e">ParseDuration</span>(<span style="color:#a6e22e">ago</span>)
      <span style="color:#a6e22e">bt</span>=<span style="color:#a6e22e">currentTime</span>.<span style="color:#a6e22e">Add</span>(<span style="color:#a6e22e">d</span>).<span style="color:#a6e22e">Unix</span>()
      <span style="color:#a6e22e">et</span>=<span style="color:#a6e22e">time</span>.<span style="color:#a6e22e">Now</span>().<span style="color:#a6e22e">Unix</span>()
   <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">&#34;开始时间《%v》结束时间《%v》\n&#34;</span>,<span style="color:#a6e22e">TimestampTransTime</span>(<span style="color:#a6e22e">bt</span>),<span style="color:#a6e22e">TimestampTransTime</span>(<span style="color:#a6e22e">et</span>))

   <span style="color:#a6e22e">f</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">os</span>.<span style="color:#a6e22e">OpenFile</span>(<span style="color:#a6e22e">LOGFILE</span>,<span style="color:#a6e22e">os</span>.<span style="color:#a6e22e">O_APPEND</span>|<span style="color:#a6e22e">os</span>.<span style="color:#a6e22e">O_CREATE</span>|<span style="color:#a6e22e">os</span>.<span style="color:#a6e22e">O_RDONLY</span>,<span style="color:#ae81ff">0644</span>)

   <span style="color:#66d9ef">defer</span> <span style="color:#a6e22e">f</span>.<span style="color:#a6e22e">Close</span>()
   <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span>{
      panic(<span style="color:#e6db74">&#34;file open error&#34;</span>)
      <span style="color:#66d9ef">return</span>
   }
   <span style="color:#a6e22e">reader</span><span style="color:#f92672">:=</span><span style="color:#a6e22e">bufio</span>.<span style="color:#a6e22e">NewReader</span>(<span style="color:#a6e22e">f</span>) <span style="color:#75715e">//默认是4096
</span><span style="color:#75715e"></span>   <span style="color:#66d9ef">for</span>  {
      <span style="color:#75715e">//读取文件
</span><span style="color:#75715e"></span>      <span style="color:#a6e22e">line</span>,<span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">reader</span>.<span style="color:#a6e22e">ReadBytes</span>(<span style="color:#e6db74">&#39;\n&#39;</span>)
      <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">==</span> <span style="color:#a6e22e">io</span>.<span style="color:#a6e22e">EOF</span>{
         <span style="color:#66d9ef">break</span>
      }
      <span style="color:#75715e">//json序列化
</span><span style="color:#75715e"></span>      <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span><span style="color:#f92672">:=</span><span style="color:#a6e22e">json</span>.<span style="color:#a6e22e">Unmarshal</span>([]byte(<span style="color:#a6e22e">line</span>),<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">nf</span>);<span style="color:#a6e22e">err</span><span style="color:#f92672">!=</span><span style="color:#66d9ef">nil</span>{
         <span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">&#34;解析json失败，当前处理行：%s&#34;</span>,<span style="color:#a6e22e">line</span>)
         panic(<span style="color:#a6e22e">err</span>)
      }
      <span style="color:#75715e">//状态码转换为整型
</span><span style="color:#75715e"></span>      <span style="color:#a6e22e">status</span>,<span style="color:#a6e22e">_</span><span style="color:#f92672">:=</span><span style="color:#a6e22e">strconv</span>.<span style="color:#a6e22e">Atoi</span>(<span style="color:#a6e22e">nf</span>.<span style="color:#a6e22e">Status</span>)

      <span style="color:#a6e22e">readTime</span><span style="color:#f92672">:=</span><span style="color:#a6e22e">UTCTransLocal</span>(<span style="color:#a6e22e">nf</span>.<span style="color:#a6e22e">Timestamp</span>)
      <span style="color:#75715e">//默认读取整个文件，不判断时间
</span><span style="color:#75715e"></span>      <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">readTime</span><span style="color:#f92672">&gt;=</span><span style="color:#a6e22e">bt</span> <span style="color:#f92672">&amp;&amp;</span> <span style="color:#a6e22e">readTime</span> <span style="color:#f92672">&lt;=</span><span style="color:#a6e22e">et</span>{
         <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">status</span><span style="color:#f92672">!=</span><span style="color:#ae81ff">200</span> <span style="color:#f92672">&amp;&amp;</span> <span style="color:#a6e22e">status</span><span style="color:#f92672">!=</span><span style="color:#ae81ff">101</span> {
            <span style="color:#a6e22e">errCounter</span><span style="color:#f92672">++</span>
         }
         <span style="color:#75715e">//{&#34;total&#34;: {&#34;200&#34;: &#34;50&#34;, &#34;101&#34;: &#34;179&#34;, &#34;500&#34;: &#34;390&#34;,&#34;404&#34;:&#34;290&#34;}}
</span><span style="color:#75715e"></span>         <span style="color:#a6e22e">tmp1</span>[<span style="color:#a6e22e">nf</span>.<span style="color:#a6e22e">Status</span>]<span style="color:#f92672">+=</span><span style="color:#ae81ff">1</span>

         <span style="color:#a6e22e">re</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">regexp</span>.<span style="color:#a6e22e">MustCompile</span>(<span style="color:#a6e22e">regx</span>)
         <span style="color:#a6e22e">match</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">re</span>.<span style="color:#a6e22e">MatchString</span>(<span style="color:#a6e22e">nf</span>.<span style="color:#a6e22e">RemoteAddr</span>)
         <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">match</span>{
            <span style="color:#66d9ef">continue</span>
         }
         <span style="color:#a6e22e">MapIpNums</span>[<span style="color:#a6e22e">nf</span>.<span style="color:#a6e22e">RemoteAddr</span>]<span style="color:#f92672">+=</span><span style="color:#ae81ff">1</span>
         <span style="color:#a6e22e">MapUrlNums</span>[<span style="color:#a6e22e">nf</span>.<span style="color:#a6e22e">RequestURI</span>]<span style="color:#f92672">+=</span><span style="color:#ae81ff">1</span>
         <span style="color:#75715e">//计算所读取的行数
</span><span style="color:#75715e"></span>         <span style="color:#a6e22e">totalCounter</span><span style="color:#f92672">++</span>
      }

   }
   <span style="color:#a6e22e">StatusMap</span>[<span style="color:#e6db74">&#34;total&#34;</span>]=<span style="color:#a6e22e">tmp1</span>
}
<span style="color:#75715e">//计算的值和阀值相比较,如果大于阀值返回true，否则为false
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">computer</span>(<span style="color:#a6e22e">errPer</span>,<span style="color:#a6e22e">threshold</span> <span style="color:#66d9ef">float64</span>) <span style="color:#66d9ef">bool</span> {
   <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">errPer</span> &gt;<span style="color:#a6e22e">threshold</span>{
      <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span>
   }
   <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">false</span>
}

<span style="color:#75715e">//percent 计算百分比,错误数/总数
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">percent</span>(<span style="color:#a6e22e">k1</span>, <span style="color:#a6e22e">k2</span> <span style="color:#66d9ef">int</span>) (<span style="color:#66d9ef">float64</span>, <span style="color:#66d9ef">bool</span>) {
   <span style="color:#a6e22e">decimal</span>.<span style="color:#a6e22e">DivisionPrecision</span>=<span style="color:#ae81ff">4</span>
   <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">k1</span><span style="color:#f92672">==</span><span style="color:#ae81ff">0</span>{
      <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Print</span>(<span style="color:#e6db74">&#34;nginx错误状态码数量是0【无错误状态码】\n&#34;</span>)
      <span style="color:#66d9ef">return</span> float64(<span style="color:#ae81ff">0</span>),<span style="color:#66d9ef">false</span>
   }
   <span style="color:#a6e22e">res</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">decimal</span>.<span style="color:#a6e22e">NewFromFloat</span>(float64(<span style="color:#a6e22e">k1</span>)).<span style="color:#a6e22e">Div</span>(<span style="color:#a6e22e">decimal</span>.<span style="color:#a6e22e">NewFromFloat</span>(float64(<span style="color:#a6e22e">k2</span>)))
   <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">&#34;Nginx错误状态码/所有状态码百分比：%v\n&#34;</span>,<span style="color:#a6e22e">res</span>)
   <span style="color:#66d9ef">return</span>  <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">Float64</span>()
}
<span style="color:#75715e">//nginx UTC格式转换为时间戳
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">UTCTransLocal</span>(<span style="color:#a6e22e">utcTime</span> <span style="color:#66d9ef">string</span>) <span style="color:#66d9ef">int64</span> {
   <span style="color:#a6e22e">t</span>, <span style="color:#a6e22e">_</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">time</span>.<span style="color:#a6e22e">Parse</span>(<span style="color:#e6db74">&#34;2006-01-02T15:04:05+08:00&#34;</span>, <span style="color:#a6e22e">utcTime</span>)
   <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">t</span>.<span style="color:#a6e22e">Unix</span>()<span style="color:#f92672">-</span><span style="color:#ae81ff">3600</span><span style="color:#f92672">*</span><span style="color:#ae81ff">8</span>
}
<span style="color:#75715e">//时间戳转换为时间
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">TimestampTransTime</span>(<span style="color:#a6e22e">timestamp</span> <span style="color:#66d9ef">int64</span>) <span style="color:#66d9ef">string</span> {
   <span style="color:#a6e22e">timeLayout</span> <span style="color:#f92672">:=</span> <span style="color:#e6db74">&#34;2006-01-02 15:04:05&#34;</span>
   <span style="color:#75715e">// time.Unix的第二个参数传递0或10结果一样，因为都不大于1e9
</span><span style="color:#75715e"></span>   <span style="color:#a6e22e">timeStr</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">time</span>.<span style="color:#a6e22e">Unix</span>(<span style="color:#a6e22e">timestamp</span>, <span style="color:#ae81ff">0</span>).<span style="color:#a6e22e">Format</span>(<span style="color:#a6e22e">timeLayout</span>)
   <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">timeStr</span>
}

<span style="color:#75715e">//按照map中值降序排，
</span><span style="color:#75715e"></span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">impounds</span> <span style="color:#66d9ef">struct</span> {
   <span style="color:#a6e22e">Key</span> <span style="color:#66d9ef">string</span>
   <span style="color:#a6e22e">Val</span> <span style="color:#66d9ef">int</span>
}
<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">SortMapByValue</span>(<span style="color:#a6e22e">m</span> <span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">int</span>)  []<span style="color:#a6e22e">impounds</span> {
   <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">l</span> []<span style="color:#a6e22e">impounds</span>
   <span style="color:#66d9ef">for</span> <span style="color:#a6e22e">k</span>,<span style="color:#a6e22e">v</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">range</span> <span style="color:#a6e22e">m</span>{
      <span style="color:#a6e22e">l</span>=append(<span style="color:#a6e22e">l</span>, <span style="color:#a6e22e">impounds</span>{<span style="color:#a6e22e">k</span>,<span style="color:#a6e22e">v</span>})
   }
   <span style="color:#a6e22e">sort</span>.<span style="color:#a6e22e">Slice</span>(<span style="color:#a6e22e">l</span>, <span style="color:#66d9ef">func</span>(<span style="color:#a6e22e">i</span>, <span style="color:#a6e22e">j</span> <span style="color:#66d9ef">int</span>) <span style="color:#66d9ef">bool</span> {
      <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">l</span>[<span style="color:#a6e22e">i</span>].<span style="color:#a6e22e">Val</span> &gt; <span style="color:#a6e22e">l</span>[<span style="color:#a6e22e">j</span>].<span style="color:#a6e22e">Val</span>  <span style="color:#75715e">// 降序
</span><span style="color:#75715e"></span>   })
   <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">l</span>

}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">Start</span>()  {
   <span style="color:#a6e22e">ReadNginxLog</span>()
   <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">&#34;nginx错误状态码数量:%v\n&#34;</span>,<span style="color:#a6e22e">errCounter</span>)
   <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">&#34;总数量:%v\n&#34;</span>,<span style="color:#a6e22e">totalCounter</span>)
   <span style="color:#a6e22e">erbium</span>,<span style="color:#a6e22e">_</span><span style="color:#f92672">:=</span><span style="color:#a6e22e">percent</span>(<span style="color:#a6e22e">errCounter</span>,<span style="color:#a6e22e">totalCounter</span>)
   <span style="color:#a6e22e">flag</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">computer</span>(<span style="color:#a6e22e">erbium</span>, <span style="color:#a6e22e">Threshold</span>)

   <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">flag</span>{
      <span style="color:#a6e22e">_</span> = <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Sprintf</span>(<span style="color:#e6db74">&#34;占比超阀值了，开始告警通知\n&#34;</span>)
      <span style="color:#75715e">//SendPhone(msg,&#34;18601301393&#34;)
</span><span style="color:#75715e"></span>   }<span style="color:#66d9ef">else</span> {
      <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">&#34;错误状态码百分比没超过阀值\n&#34;</span>)
   }
   <span style="color:#a6e22e">result</span><span style="color:#f92672">:=</span><span style="color:#a6e22e">SortMapByValue</span>(<span style="color:#a6e22e">MapIpNums</span>)
   <span style="color:#a6e22e">res</span><span style="color:#f92672">:=</span><span style="color:#a6e22e">SortMapByValue</span>(<span style="color:#a6e22e">MapUrlNums</span>)
   <span style="color:#75715e">//判断切片长度，防止溢出,以及切片长度不能超过top 的值，不然也会溢出
</span><span style="color:#75715e"></span>   <span style="color:#66d9ef">if</span> len(<span style="color:#a6e22e">result</span>)<span style="color:#f92672">==</span><span style="color:#ae81ff">0</span><span style="color:#f92672">||</span>len(<span style="color:#a6e22e">res</span>)<span style="color:#f92672">==</span><span style="color:#ae81ff">0</span><span style="color:#f92672">||</span>len(<span style="color:#a6e22e">result</span>)&lt;<span style="color:#a6e22e">Top</span><span style="color:#f92672">||</span>len(<span style="color:#a6e22e">res</span>)&lt;<span style="color:#a6e22e">Top</span>{
      <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Print</span>(<span style="color:#e6db74">&#34;top值太大了&#34;</span>)
      <span style="color:#66d9ef">return</span>

   }
   <span style="color:#66d9ef">for</span> <span style="color:#a6e22e">_</span>,<span style="color:#a6e22e">v</span><span style="color:#f92672">:=</span> <span style="color:#66d9ef">range</span> <span style="color:#a6e22e">result</span>[<span style="color:#ae81ff">0</span>:<span style="color:#a6e22e">Top</span>]{
      <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">&#34;\033[0;35;40m%-30v \t %-30v\n\033[0m&#34;</span>,<span style="color:#a6e22e">v</span>.<span style="color:#a6e22e">Key</span>,<span style="color:#a6e22e">v</span>.<span style="color:#a6e22e">Val</span>)
   }
   <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;---------------------------------------&#34;</span>)
   <span style="color:#66d9ef">for</span> <span style="color:#a6e22e">_</span>,<span style="color:#a6e22e">v</span><span style="color:#f92672">:=</span> <span style="color:#66d9ef">range</span> <span style="color:#a6e22e">res</span>[<span style="color:#ae81ff">0</span>:<span style="color:#a6e22e">Top</span>]{
      <span style="color:#75715e">//fmt.Printf(&#34;\033[0;32;40m \t %v \033[0m\033[0;31;40m %v \n \033[0m&#34;,v.Key,v.Val)
</span><span style="color:#75715e"></span>      <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">&#34;\033[0;32;40m%-50v \t %-70v\n\033[0m&#34;</span>,<span style="color:#a6e22e">v</span>.<span style="color:#a6e22e">Key</span>,<span style="color:#a6e22e">v</span>.<span style="color:#a6e22e">Val</span>)
   }
   <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">StatusMap</span>)
}
<span style="color:#75715e">//格式化输出
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">OutPrintf</span>(<span style="color:#a6e22e">m</span> <span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">int</span>){

}
</code></pre></td></tr></table>
</div>
</div><p>执行结果：

        <a data-fancybox="gallery" href="https://gitee.com/stto_32/img/raw/master/20211209145442.png">
            <img class="mx-auto" alt="image-20211209145442163" src="https://gitee.com/stto_32/img/raw/master/20211209145442.png" />
        </a>
    </p>
        </div>

        
<div class="post-archive">
    <ul class="post-copyright">
        <li><strong>原文作者：</strong><a rel="author" href="https://stto_32.gitee.io/site">仨哥</a></li>
        <li style="word-break:break-all"><strong>原文链接：</strong><a href="https://stto_32.gitee.io/site/post/go%E4%B9%8Bnginx%E6%97%A5%E5%BF%97%E5%88%86%E6%9E%90/">https://stto_32.gitee.io/site/post/go%E4%B9%8Bnginx%E6%97%A5%E5%BF%97%E5%88%86%E6%9E%90/</a></li>
        <li><strong>版权声明：</strong>本作品采用<a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/">知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议</a>进行许可，非商业转载请注明出处（作者，原文链接），商业转载请联系作者获得授权。</li>
    </ul>
</div>
<br/>



        

<div class="post-archive">
    <h2>See Also</h2>
    <ul class="listing">
        
        <li><a href="/site/post/casbin/">casbin权限管理</a></li>
        
        <li><a href="/site/post/cloudstack&#43;kvm/">cloudstack-4.1.5版本最全入门笔记【2022】</a></li>
        
        <li><a href="/site/post/glusterfs%E5%AE%89%E8%A3%85/">glusterfs安装</a></li>
        
        <li><a href="/site/post/%E4%BA%BA%E4%BA%BA%E8%83%BD%E7%9C%8B%E6%87%82%E7%9A%84Async-Go/">go异步</a></li>
        
        <li><a href="/site/post/k8s%E7%8E%AF%E5%A2%83%E4%B8%8Bphp/">k8s环境php环境制品</a></li>
        
    </ul>
</div>


        <div class="post-meta meta-tags">
            
            没有标签
            
        </div>
    </article>
    
    <div id="disqus_thread"></div>
<script type="application/javascript">
    var disqus_config = function () {
    
    
    
    };
    (function() {
        if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
            document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
            return;
        }
        var d = document, s = d.createElement('script'); s.async = true;
        s.src = '//' + "xull" + '.disqus.com/embed.js';
        s.setAttribute('data-timestamp', +new Date());
        (d.head || d.body).appendChild(s);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>

    
    
    <div class="post bg-white">
      <script src="https://utteranc.es/client.js"
            repo= "51op/hugoblogtalks"
            issue-term="pathname"
            theme="github-light"
            crossorigin="anonymous"
            async>
      </script>
    </div>
    
</div>

                    <footer id="footer">
    <div>
        &copy; 2022 <a href="https://stto_32.gitee.io/site">Sang的博客 By 仨哥</a>
        
    </div>
    <br />
    <div>
        <div class="github-badge">
            <a href="https://gohugo.io/" target="_black" rel="nofollow"><span class="badge-subject">Powered by</span><span class="badge-value bg-blue">Hugo</span></a>
        </div>
        <div class="github-badge">
            <a href="https://github.com/flysnow-org/maupassant-hugo" target="_black"><span class="badge-subject">Theme</span><span class="badge-value bg-yellowgreen">Maupassant</span></a>
        </div>
    </div>
</footer>


    
    <script type="text/javascript">
        window.MathJax = {
            tex2jax: {
                inlineMath: [['$', '$']],
                processEscapes: true
                }
            };
    </script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML' async></script><script src="https://cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>

<a id="rocket" href="#top"></a>
<script type="text/javascript" src='/site/js/totop.js?v=0.0.0' async=""></script>



    <script type="text/javascript" src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>




    <script src='/site/js/douban.js'></script>

                </div>

                <div id="secondary">
    <section class="widget">
        <form id="search" action='https://stto_32.gitee.io/site/search/' method="get" accept-charset="utf-8" target="_blank" _lpchecked="1">
      
      <input type="text" name="q" maxlength="20" placeholder="Search">
      <input type="hidden" name="sitesearch" value="https://stto_32.gitee.io/site">
      <button type="submit" class="submit icon-search"></button>
</form>
    </section>
    
    <section class="widget">
        <h3 class="widget-title">最近文章</h3>
<ul class="widget-list">
    
    <li>
        <a href="https://stto_32.gitee.io/site/post/calico/" title="Calico网络之BGP详解">Calico网络之BGP详解</a>
    </li>
    
    <li>
        <a href="https://stto_32.gitee.io/site/post/casbin/" title="casbin权限管理">casbin权限管理</a>
    </li>
    
    <li>
        <a href="https://stto_32.gitee.io/site/post/cloudstack&#43;kvm/" title="cloudstack-4.1.5版本最全入门笔记【2022】">cloudstack-4.1.5版本最全入门笔记【2022】</a>
    </li>
    
    <li>
        <a href="https://stto_32.gitee.io/site/post/glusterfs%E5%AE%89%E8%A3%85/" title="glusterfs安装">glusterfs安装</a>
    </li>
    
    <li>
        <a href="https://stto_32.gitee.io/site/post/go%E4%B9%8Bnginx%E6%97%A5%E5%BF%97%E5%88%86%E6%9E%90/" title="go之nginx日志分析">go之nginx日志分析</a>
    </li>
    
    <li>
        <a href="https://stto_32.gitee.io/site/post/%E4%BA%BA%E4%BA%BA%E8%83%BD%E7%9C%8B%E6%87%82%E7%9A%84Async-Go/" title="go异步">go异步</a>
    </li>
    
    <li>
        <a href="https://stto_32.gitee.io/site/post/k8s%E7%8E%AF%E5%A2%83%E4%B8%8Bphp/" title="k8s环境php环境制品">k8s环境php环境制品</a>
    </li>
    
    <li>
        <a href="https://stto_32.gitee.io/site/post/kubernetes-client%E4%B9%8Bpython%E8%AF%A6%E7%BB%86%E6%8E%A5%E5%8F%A3%E8%B0%83%E7%94%A8/" title="kubernetes-client之python详细接口调用">kubernetes-client之python详细接口调用</a>
    </li>
    
    <li>
        <a href="https://stto_32.gitee.io/site/post/mac%E5%B7%A5%E5%85%B7/" title="mac工具">mac工具</a>
    </li>
    
    <li>
        <a href="https://stto_32.gitee.io/site/post/%E5%9F%BA%E4%BA%8E1.22.1%E7%89%88%E6%9C%AC%E7%9A%84k8s%E9%83%A8%E7%BD%B2/" title="【2022】最详细的k8s基于1.22.1版本部署">【2022】最详细的k8s基于1.22.1版本部署</a>
    </li>
    
</ul>
    </section>

    

    <section class="widget">
        <h3 class="widget-title"><a href='/site/categories/'>分类</a></h3>
<ul class="widget-list">
    
</ul>
    </section>

    <section class="widget">
        <h3 class="widget-title"><a href='/site/tags/'>标签</a></h3>
<div class="tagcloud">
    
    <a href="https://stto_32.gitee.io/site/tags/cloudstack/">cloudstack</a>
    
    <a href="https://stto_32.gitee.io/site/tags/k8s/">k8s</a>
    
    <a href="https://stto_32.gitee.io/site/tags/kvm/">kvm</a>
    
    <a href="https://stto_32.gitee.io/site/tags/php/">php</a>
    
    <a href="https://stto_32.gitee.io/site/tags/shell/">shell</a>
    
    <a href="https://stto_32.gitee.io/site/tags/%E4%BA%91%E5%8E%9F%E7%94%9F/">云原生</a>
    
</div>
    </section>

    
<section class="widget">
    <h3 class="widget-title">友情链接</h3>
    <ul class="widget-list">
        
        <li>
            <a target="_blank" href="https://www.cnblogs.com/xull0651/" title="Sang的博客">Sang的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="https://juejin.cn/user/2796746682943495" title="掘金">掘金</a>
        </li>
        
    </ul>
</section>


    <section class="widget">
        <h3 class="widget-title">其它</h3>
        <ul class="widget-list">
            <li><a href="https://stto_32.gitee.io/site/index.xml">文章 RSS</a></li>
        </ul>
    </section>
</div>
            </div>
        </div>
    </div>
</body>

</html>